<template>
	<view class="container">
		<view class="left-bottom-sign"></view>
		<view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view>
		<view class="right-top-sign"></view>
		<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
		<view class="wrapper">
			<view class="left-top-sign">LOGIN</view>
			<view class="welcome">
				欢迎回来！
			</view>
			<button class="confirm-btn" @click="login">登录</button>
		</view>
		<view class="register-section">
			还没有账号?
			<text @click="toRegist">马上注册</text>
		</view>
		<view class='dialog-mask' :class="[PhoneID==0?'':'none']"></view>
		<view class='dialog-info' :class="[PhoneID==0?'':'none']">
			<view class='dialog-title'>提示</view>
			<view class='dialog-content'>获取手机号码,用以登录使用会员卡功能</view>
			<view class='dialog-footer'>
				<button class='dialog-btn' @click="cancel">取消</button>
				<button class='dialog-btn' open-type="getPhoneNumber" @getphonenumber='getPhoneNumber'>确定</button>
			</view>
		</view>
	</view>

</template>

<script>
	// const jweixin = require('../../utils/jweixin-module')	
	import {
		mapMutations
	} from 'vuex';

	export default {
		data() {
			return {
				mobile: '',
				password: '',
				logining: false,
				PhoneID: 0, //手机号码是否存在 1=>存在 0=>不存在
				code: ''
			}
		},
		onLoad() {
			this.$Url = uni.getStorageSync('URL')
			var pages = getCurrentPages(); //当前页
			var beforePage = pages[pages.length - 2]; //上个页面
			// this.$beforePage = "/" + beforePage.route + "?scene=" + beforePage.options.scene;
			this.$beforePage = beforePage.$page.fullPath; // pages/user/MemberUser
			let userInfo = [];
			if (uni.getStorageSync('MuserInfo') != "") {
				userInfo = uni.getStorageSync('MuserInfo');
			}
			uni.login({
				provider: 'weixin',
				success: (res) => {
					this.code = res.code;
				}
			})
			if (userInfo.length == 0) {
				uni.navigateTo({
					url: '../userInfo/userInfo',
				})
				return;
			}
		},
		methods: {
			...mapMutations(['login']),
			inputChange(e) {
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},
			navBack() {
				uni.navigateBack();
			},
			toRegist() {
				this.$api.msg('去注册');
			},
			login(){
				this.PhoneID = 0;
			},
			onGetUserInfo() {
				uni.showLoading({
					title: '加载中'
				})
				this.logining = true;
				// let loginID = 0;//记录登录的状态
				uni.login({
					provider: 'weixin',
					success: (res) => {
						let code = res.code;
						//抛异常
						try {
							this.$util.Login(code).then(res => {
								this.PhoneID = uni.getStorageSync('PhoneID');
								if (res == 1 && this.PhoneID == 1) {
									uni.hideLoading();
									// uni.reLaunch({
									// 	url:'../user/user',
									// })
									uni.reLaunch({
										url: this.$beforePage,
									})
								} else {
									uni.hideLoading();
								}
							});
						} catch (e) {
							console.log('err', e);
						}
					}
				})
			},
			//获取电话号码
			getPhoneNumber(e) {
				let detail = e.detail;
				let self = this;
				// return new Promise((resolve, reject) => {
				uni.checkSession({
					success: res => {
						this.PhoneID = 1;
						if (detail.errMsg == 'getPhoneNumber:fail:user cancel') {
							// wx.removeStorageSync("Token");
						} else {
							if (detail.errMsg == 'getPhoneNumber:fail user deny') {
								FPhone = 1
								resolve(FPhone); //输出结果 
							} else { //同意授权
								let token = uni.getStorageSync('Token'),
									cookie = uni.getStorageSync('Cookies');
								uni.request({
									url: this.$Url + 'WeChatShop/WxPhoneDecrypt',
									data: {
										EncryptedData: detail.encryptedData,
										Iv: detail.iv,
										AppId: uni.getAccountInfoSync().miniProgram.appId,
										Code: this.code
									},
									method: 'POST',
									header: {
										'content-type': 'application/json', // 默认值
										'cookie': cookie,
										'token': token,
									},
									success: (res) => {
										// uni.reLaunch({
										// 	url:'../user/user',
										// })
										uni.setStorageSync('phone', res.data.ResultData)
										self.onGetUserInfo();
										// uni.reLaunch({
										// 	url: this.$beforePage,
										// })
									},
									fail: (res) => {

									}
								})
							}
						}
					},
					fail: function(res) {

						// 发送 res.code 到后台换取 openId, sessionKey, unionId
						// wx.login({
						// 	success: res => {
						// 		loginType.onLogin(res.code);
						// 	}
						// })
						self.onGetUserInfo();
					}
				});
				// });
			},
			cancel(){
				this.PhoneID = 1;
			},
			Login() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						let parentid = ''; //推荐人
						//推荐人
						let parentID = uni.getStorageSync('parentID');
						var lastHostid = uni.getStorageSync('lastHostid'); //旧的
						var appid = uni.getAccountInfoSync().miniProgram.appId;
						// let stockId = 0 uni.getStorageSync('stockId');
						let stockId = 0 ;
						var options = JSON.stringify(uni.getStorageSync('options'));
						if (parentID != '') {
							parentid = parentID
						} else {
							parentid = 0;
						}
						let phone = uni.getStorageSync("phone");
						let url = 'WeChatShop/OnLogin?code=' + res.code + '&appid=' + appid + '&hostid=' + lastHostid +
							'&stockid=' + stockId + '&scene=' + options + '&parentID=' + parentid + '&phone=' + phone;
						this.$util.login3(url).then(res => {
							uni.setStorageSync('LoginID', res.status)
							if (res.status == 1) {
								// uni.reLaunch({
								// 	url:'../user/user',
								// })
								uni.reLaunch({
									url: this.$beforePage,
								})
							}
							uni.hideLoading();
						})
					},
				})
			}
		},

	}
</script>

<style lang='scss'>
	page {
		background: #fff;
	}

	.container {
		padding-top: 115px;
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: #fff;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}

	.left-top-sign {
		font-size: 120upx;
		color: $page-color-base;
		position: relative;
		left: -16upx;
	}

	.right-top-sign {
		position: absolute;
		top: 80upx;
		right: -30upx;
		z-index: 95;

		&:before,
		&:after {
			display: block;
			content: "";
			width: 400upx;
			height: 80upx;
			background: #b4f3e2;
		}

		&:before {
			transform: rotate(50deg);
			border-radius: 0 50px 0 0;
		}

		&:after {
			position: absolute;
			right: -198upx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 50px 0 0 0;
			/* background: pink; */
		}
	}

	.left-bottom-sign {
		position: absolute;
		left: -270upx;
		bottom: -320upx;
		border: 100upx solid #d0d1fd;
		border-radius: 50%;
		padding: 180upx;
	}

	.welcome {
		position: relative;
		left: 50upx;
		top: -90upx;
		font-size: 46upx;
		color: #555;
		text-shadow: 1px 0px 1px rgba(0, 0, 0, .3);
	}

	.input-content {
		padding: 0 60upx;
	}

	.input-item {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
		padding: 0 30upx;
		background: $page-color-light;
		height: 120upx;
		border-radius: 4px;
		margin-bottom: 50upx;

		&:last-child {
			margin-bottom: 0;
		}

		.tit {
			height: 50upx;
			line-height: 56upx;
			font-size: $font-sm+2upx;
			color: $font-color-base;
		}

		input {
			height: 60upx;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			width: 100%;
		}
	}

	.confirm-btn {
		width: 630upx;
		height: 76upx;
		line-height: 76upx;
		border-radius: 50px;
		margin-top: 70upx;
		background: $uni-color-primary;
		color: #fff;
		font-size: $font-lg;

		&:after {
			border-radius: 100px;
		}
	}

	.forget-section {
		font-size: $font-sm+2upx;
		color: $font-color-spec;
		text-align: center;
		margin-top: 40upx;
	}

	.register-section {
		position: absolute;
		left: 0;
		bottom: 50upx;
		width: 100%;
		font-size: $font-sm+2upx;
		color: $font-color-base;
		text-align: center;

		text {
			color: $font-color-spec;
			margin-left: 10upx;
		}
	}

	/* 手机号码 */
	.dialog-mask {
		position: fixed;
		z-index: 1000;
		top: 0;
		right: 0;
		left: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.3);
	}

	.dialog-info {
		position: fixed;
		z-index: 5000;
		width: 80%;
		max-width: 600rpx;
		top: 50%;
		left: 50%;
		-webkit-transform: translate(-50%, -50%);
		transform: translate(-50%, -50%);
		background-color: #fff;
		text-align: center;
		border-radius: 3px;
		overflow: hidden;
	}

	.dialog-title {
		font-size: 36rpx;
		padding: 30rpx 30rpx 10rpx;
	}

	.dialog-content {
		padding: 10rpx 30rpx 20rpx;
		min-height: 80rpx;
		font-size: 32rpx;
		line-height: 1.3;
		word-wrap: break-word;
		word-break: break-all;
		color: #999;
	}

	.dialog-footer {
		display: flex;
		align-items: center;
		position: relative;
		line-height: 90rpx;
		font-size: 34rpx;
	}

	.dialog-btn {
		display: block;
		-webkit-flex: 1;
		flex: 1;
		position: relative;
		color: #3cc51f;
	}

	.none {
		display: none;
	}
</style>
